<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
const student = ref([
    {
        "studentId": "",
        "sname": "",
        "sgrade": "",
        "major":"",
        "status":"",
        "createTime": "",
        "updateTime": ""
    }
])

//声明一个异步的函数 页面加载完毕之后 调用articleCategoryListService方法来获取所有文章的分类列表 调用这个函数时同步等待他返回的结果
import {studentListService,studentAddService,studentUpdateService,studentDeleteService,studentSelectByName} from '@/api/student.js'
const studentList = async(keyword='')=>{
    let result
    if(keyword){
        result = await studentSelectByName(keyword);//有关键字就调用
    }
    else{
        result = await studentListService();//没有关键字
    }
    student.value=result.data//result.data列表数据 赋值给student
}
studentList();//调用


//控制添加分类弹窗 默认不显示
const dialogVisible = ref(false)

const searchkeyword = ref('')//关键字
//添加分类数据模型
const studentModel = ref({
    studentId:'',
    sname: '',
    sgrade: null,
    major:'',
    status:null,
})
//添加分类表单校验
const rules = {
    studentId: [
        { required: true, message: '请输入学生学号', trigger: 'blur' },
    ],
    sname: [
        { required: true, message: '请输入学生姓名', trigger: 'blur' },
    ],
    sgrade: [
        { required: true, message: '请输入学生年级', trigger: 'blur' },
    ],
    major: [
        { required: true, message: '请输入学生所在系', trigger: 'blur' },
    ],
    status: [
        { required: true, message: '请输入学生档案在校情况', trigger: 'blur' },
    ]
}

//调用接口 添加表单
import {ElMessage} from 'element-plus'
const addStudent = async()=>{
    //调用接口 同步等待异步调用的结果
   let result =  await studentAddService(studentModel.value)
   if(result.code == 1){
        ElMessage.error('插入相同学号学生');
        return;
   }
   ElMessage.success(result.msg? result.msg:'添加成功')// msg有东西就输出 没东西就添加成功

   //调用获取所有文章文类的函数
   studentList();
   //消失当前弹窗
   dialogVisible.value=false
}

//定义变量 控制标题的展示
const title = ref('')

//展示编辑弹窗 数据回写 把原数据展示出来
const showDialog = (row)=>{
    console.log('点击编辑的行数据 row：', row);
    dialogVisible.value=true;title.value = '编辑学生'
    //数据拷贝
    studentModel.value.studentId = row.studentId;
    studentModel.value.sname = row.sname;
    studentModel.value.sgrade = row.sgrade;
    studentModel.value.major = row.major;
    studentModel.value.status = row.status;
}

//更改学生
const updateStudent = async() =>{
    //调用接口
    console.log('准备提交修改的 studentModel 值：', studentModel.value); 
    let result = await studentUpdateService(studentModel.value);
    ElMessage.success(result.msg?result.msg:'修改成功')

    //调用获取所有学生函数
    studentList();
    
    dialogVisible.value = false;
}

//删除学生
import {ElMessageBox} from 'element-plus'
const deleteStudent = async(row)=>{
    //提示用户 展示确认框

        ElMessageBox.confirm(
    '确认删除该学生吗?',
    'Warning',
    {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning',
    }
    )
    .then(async() => {
        //调用接口
        let result = await studentDeleteService(row.studentId)
        if(result.code==1){
            ElMessage.error("请先删除总成绩管理的对应学生");
            return;
        }
    ElMessage({
        type:'success',
        message: 'Delete completed',
    })
    //刷新列表
    studentList();
    })
    .catch(() => {
    ElMessage({
        type: 'info',
        message: 'Delete canceled',
    })
    })
}




//根据关键字查找去掉空格
const handleSearch = ()=>{
    studentList(searchkeyword.value.trim())
}

const reset = ()=>{
    searchkeyword.value='';//置空
    studentList();
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>学生管理</span>
                <div class="extra">
                    <!-- 添加搜索UI -->
                    <div style="display: flex; align-items: center; margin-bottom: 10px;">
                        <el-input 
                            v-model="searchkeyword" 
                            placeholder="请输入姓名"
                            style="width: 200px; margin-right: 10px"
                            @keyup.enter="handleSearch"
                        />
                        <el-button type="primary" @click="handleSearch">搜索</el-button>
                        <el-button @click="reset">重置</el-button>
                    </div>
                    <el-button type="primary" @click="dialogVisible=true;title = '添加学生'">添加学生</el-button>
                </div>
            </div>
        </template>
        <el-table :data="student" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <el-table-column label="学生学号" prop="studentId"></el-table-column>
            <el-table-column label="学生姓名" prop="sname"></el-table-column>
            <el-table-column label="学生年级" prop="sgrade"></el-table-column>
            <el-table-column label="学生专业" prop="major"></el-table-column>
     <el-table-column label="学生档案状态" prop="status">
                <template #default="{ row }">
                    <el-tag :type="row.status === 1 ? 'success' : 'warning'">
                        {{ row.status === 1 ? '在校' : '休学' }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="100">
                <!--表示这一行的数据-->
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteStudent(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加分类弹窗 -->
<el-dialog v-model="dialogVisible" :title="title" width="30%">
    <el-form :model="studentModel" :rules="rules" label-width="100px" style="padding-right: 30px">
        <el-form-item label="学生学号" prop="studentId">
            <el-input v-model="studentModel.studentId" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="学生姓名" prop="sname">
            <el-input v-model="studentModel.sname" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="学生年级" prop="sgrade">
            <el-input v-model="studentModel.sgrade" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="学生专业" prop="major">
            <el-input v-model="studentModel.major" minlength="1" maxlength="15"></el-input>
        </el-form-item>
        <el-form-item label="学生档案状态" prop="status">
                    <el-select v-model="studentModel.status" placeholder="请选择档案状态" style="width: 100%">
                        <el-option label="在校" value="1"></el-option>
                        <el-option label="休学" value="0"></el-option>
                    </el-select>
        </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <el-button type="primary" @click="title=='添加学生' ?addStudent():updateStudent()"> 确认 </el-button>
        </span>
    </template>
</el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>